package com.vgooo.client.tool.branch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.Area;
import com.vgooo.bean.Branch;
import com.vgooo.client.Main;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.ViewLayout;
import com.vgooo.client.tool.branch.BranchAdapter.ChildViewHolder;
import com.vgooo.client.ui.HorizontalListView;
import com.vgooo.client.ui.SortAdapter;
import com.vgooo.client.ui.VGExpandableListView;
import com.vgooo.util.DebugTools;
import com.vgooo.util.GSonTools;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.UmengUtils;

public class BrandsMainActivity extends VGActivity {

	private static String className = "BrandsMainActivity";
	
	
	private Data data ;
	
	private List<Area> areas ;//店铺区域信息
	private String currentAreas = "";
	private VGExpandableListView branchView;// 店铺列表
	private BranchAdapter bAdapter; // 店铺列表适配器
	private SortAdapter brandAdapter;;// 品牌列表适配器
	private View moreBrands ;
	
	private TextView area ;//显示已选地区
	HorizontalListView brandLayout ;
//	private List<TextView> tvs ;
	
	private int currentArea = 0;
	
	private Map<String,Data> datas ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_branch);
		initView();
		addListener();
		isFirst = true ;
//		new DataSAsyncLoader(cparent).execute(null);
		init() ;

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		super.onResume();
		if(datas==null||datas.isEmpty()){
			init() ;
		}

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		branchView = (VGExpandableListView) findViewById(R.id.tool_branch_expandablelistview);
		branchView.setGroupIndicator(null);
		brandLayout = (HorizontalListView) findViewById(R.id.tool_branch_area_select);
		area = (TextView) findViewById(R.id.tool_branch_area) ;

		moreBrands = findViewById(R.id.mobile_main_brands_sort_more);

	}

	@Override
	public void setupView() {
		// TODO Auto-generated method stub
		bAdapter = new BranchAdapter(data, this);
		branchView.setAdapter(bAdapter);
		
		if(isFirst){
			brandAdapter = new SortAdapter(areas, R.layout.bartags_item, R.id.brand_tv, moreBrands, context);
			brandLayout.setAdapter(brandAdapter);
			area.setText("全部") ;
			isFirst = false ;
		}

		
		new AsyncTask<String, String, String>(){

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				for(Area area :areas){
					try {
						if(datas.get(area.id)==null){
							getBranchInfo(area.id);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						UmengUtils.onError(context, e.toString());
					}
				}
				return null;
			}}.execute("");
		
		
		
	}

	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		branchView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Branch branch = (Branch) bAdapter.getChild(groupPosition,
						childPosition);
				ChildViewHolder holder = (ChildViewHolder) v.getTag() ;
				DebugTools.log(className, "OnChildClickListener id = "
						+ branch.branch_id + " name = " + branch.branch_name);
				branch.isDetail = !branch.isDetail ;
				if(branch.isDetail){
					holder.layout.setVisibility(View.VISIBLE);
					holder.ivDetail.setVisibility(View.GONE);
					holder.tvMap.setVisibility(View.VISIBLE);
				}else{
					holder.layout.setVisibility(View.GONE);
					holder.ivDetail.setVisibility(View.VISIBLE);
					holder.tvMap.setVisibility(View.GONE);
				}
				return false;
			}
		});
		
		brandLayout.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> v, View view, int position,
					long arg3) {
				// TODO Auto-generated method stub
				try {
					
					int index = position;
					if(index == currentArea){
						return ;
					}
					Area aread = areas.get(index);
					currentAreas = aread.id;
					area.setText(aread.area_name ) ;
					Data data = datas.get(currentAreas);
					if(data!=null&&data.parents!=null&&data.childs!=null){
						bAdapter.dataChanage(data);
					}else{
//						new DataSAsyncLoader(cparent).execute(null);
						init() ;
					}
//					getBranchInfo(aread.id) ;
					brandAdapter.tvs.get(currentArea).setBackgroundResource(0);
					currentArea = index ;
					brandAdapter.tvs.get(currentArea).setBackgroundResource(R.drawable.sort_text_select_bg);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					UmengUtils.onError(context, e.toString());
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void flashView() {
		// TODO Auto-generated method stub

	}

	@Override
	public void getData(){
		// TODO Auto-generated method stub
		try {
			Data data = getBranchInfo(currentAreas);
			this.data = data ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e) ;
		}
	}

	@Override
	public void doClick(View v){
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		
		case R.id.bartags_item_layout:
			
			break ;
			
		case R.id.branch_item_child_view_map:
			String viewTag = (String) v.getTag() ;
			Main.branchMapIntent.putExtra("link", viewTag);
			ViewLayout.setViewPre(true, "查看地图", ViewLayout.TAB_TAG_BRANCHS_MAP,cparent);
			break ;

		default:
			break;
		}

	}
	
	
	public Data getBranchInfo(String area)throws Exception {
		String urlPath = HttpUtils.BASE_URI + "branch?id=" + area;
		String jsonString = HttpUtils.getJsonString(urlPath);
		Map<String, Object> map = GSonTools.getObjectMaps(jsonString);
		List<Object> lists =  (List<Object>) map.get("branchs");
		List<String> parents = new ArrayList<String>();
		List<List<Branch>> childs = new ArrayList<List<Branch>>();
		
		if(isFirst&&areas==null||areas.isEmpty()){
			List<Map<String, Object>> areasList = (List<Map<String, Object>>) map.get("areas"); 
			
			areas = new ArrayList<Area>(areasList.size()) ;
			Area ar = new Area() ;
			ar.id = "" ;
			ar.area_name = "全部";
			ar.brand_name = "全部";
			areas.add(ar);
			for(Map<String, Object> areaInfo :areasList){
				Area a = new Area() ;
				a.id = (String) areaInfo.get("id");
				a.area_name = (String) areaInfo.get("area_name")+"地区";
				a.brand_name = (String) areaInfo.get("area_name")+"地区";
				areas.add(a);
			}
			Area ar2 = new Area() ;
			ar2.id = "" ;
			ar2.area_name = "";
			ar2.brand_name = "";
			areas.add(ar2);
		}
		Data data = null ;
		for (Object obj : lists) {
			// System.out.println(obj);
			List<Object> list1 = (List<Object>) obj;
			for (Object obj1 : list1) {
				Map<String, Object> m1 = (Map<String, Object>) obj1;
				parents.add((String) m1.get("bc_name"));
				List<Map<String, Object>> l = (List<Map<String, Object>>) m1
						.get("b");
				List<Branch> branchs = new ArrayList<Branch>();
				for (Map<String, Object> m2 : l) {
					Branch branch = new Branch();
					branch.branch_id = (String) m2.get("branch_id");
					branch.branch_name = (String) m2.get("branch_name");
					branch.branch_tel = (String) m2.get("branch_tel");
					branch.branch_addr = (String) m2.get("branch_addr");
					branch.branch_time = (String) m2.get("branch_time");
					branch.branch_jw = (String) m2.get("branch_jw");
					Map<String ,String > traffic = (Map<String, String>) m2.get("traffic") ;
					branch.bus = (String) traffic.get("bus");
					branch.metro = (String) traffic.get("metro");
					if(branch.metro==null){
						branch.metro = "";
					}
					branchs.add(branch);
				}
				childs.add(branchs);
			}
			if(isFirst){
				datas = new HashMap<String, Data>() ;
				data = new Data(parents, childs) ;
				datas.put(area, data) ;
			}else{
				data = new Data(parents, childs) ;
				datas.put(area, data) ;
			}
			
		}
		
		
		

		List<Map<String, Object>> list = (List<Map<String, Object>>) map
				.get("branchs_service");
		parents.add("客服中心");
		List<Branch> branchs = new ArrayList<Branch>();
		for (Map<String, Object> m : list) {
			Branch branch = new Branch();
			branch.branch_id = (String) m.get("branch_id");
			branch.branch_name = (String) m.get("branch_name");
			branch.branch_tel = (String) m.get("branch_tel");
			branch.branch_addr = (String) m.get("branch_addr");
			branch.branch_time = (String) m.get("branch_time");
			branch.branch_jw = (String) m.get("branch_jw");
			Map<String ,String > traffic = (Map<String, String>) m.get("traffic") ;
			branch.bus = (String) traffic.get("bus");
			branch.metro = (String) traffic.get("metro");
			if(branch.metro==null){
				branch.metro = "";
			}
			branchs.add(branch);
		}
		childs.add(branchs);
		
		return data;
	}
	
	
	public class Data{
		 List<String> parents;// 店铺父列表数据
		 List<List<Branch>> childs;// 店铺子列表数据
		public Data(List<String> parents, List<List<Branch>> childs) {
			super();
			this.parents = parents;
			this.childs = childs;
		}
		
	}

}
